package interactor;

import context.*;

import mainFrame.DrawPanel;

import java.awt.Graphics2D;
import java.awt.image.ImageObserver;

import object2D.PathObject;

import org.json.JSONException;

import util.PixelPoint;

// ********************************************************************
// *** CLASS: DrawParallelogramInteractor
// ********************************************************************
public class DrawParallelogramInteractor extends Interactor implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="interactor.DrawParallelogramInteractor";

  public String getMouseModeMnemonic() { return("Draw Parallelogram"); }
  private PathObject polygon; // !null when drawing
  private PixelPoint p0;
  private PixelPoint p1;
  private PixelPoint p2;
  private PixelPoint p3;

  // ******************************************************************
  // *** CONSTRUCTOR: DrawParallelogramInteractor
  // ******************************************************************
  public DrawParallelogramInteractor(DrawPanel drawPanel)
  { super(drawPanel,DRAW_PARALLELOGRAM); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g,ImageObserver imageObserver)
  {	if(polygon!=null) polygon.paint(g,imageObserver); }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  { if(polygon!=null)
    { if(p2==null)
      { p1.setX(mouseCurrent.getX());
        p1.setY(mouseCurrent.getY());
      }
      else
      { p2.setX(mouseCurrent.getX());
        p2.setY(mouseCurrent.getY());
        double dx=p1.getX()-p0.getX();
        double dy=p1.getY()-p0.getY();
   	    p3.setX(p2.getX()-dx);
   	    p3.setY(p2.getY()-dy);
      }
	  drawPanel.repaint();
	}
  }

  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(PixelPoint p)
  { if(polygon==null)
	{ p0=p; p1=p.copy(); p2=null; p3=null;
	  try { polygon=new PathObject(drawPanel.getObjectModel(),p0); }
	  catch(JSONException e) { e.printStackTrace(); }
	  polygon.add(p1);
	}
	else
	{ p2=p; p3=p.copy();
	  polygon.add(p2);
	  polygon.add(p3);
	  polygon.setClosedPath(true);
    }
	drawPanel.repaint();
  }
  protected void mouseRightPressed(PixelPoint p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(PixelPoint p)
  { if(polygon!=null)
    { if(p2!=null)
      { drawObjectDone(polygon,"PARALLELOGRAM");  
        polygon=null;
      } else mouseStart=p;
      drawPanel.repaint();
    }
  }
  protected void mouseRightReleased(PixelPoint p) {}

}